$("#file").on('change',function(){
    //选择文件选中的状态
    var files = this.files;
    console.log(files)
    //上传文件
    var fd = new FormData();
    //渲染
    fd.append('avatar',files[0]);
    $.ajax({
        type:'post',
        url:'http://localhost:3000/upload',
        data: fd,
        contentType: false,
        processData: false,
        success:function(res){
            console.log(res[0].avatar);
			$('#image').val(res[0].avatar);
        }
    })
})

//提交行为发生时  上传图片
$("#slidesForm").on('submit',function(e){     
    var res = $(this).serialize();
    $.ajax({
        type:'post',
        url:'http://localhost:3000/slides',
        data: res,
        success: function(){
            location.reload();
        }
    })
    return false;
})

//索要图片
$.ajax({
	type: 'get',
	url: 'http://localhost:3000/slides',
	success: function (res) {
		console.log(res)
		var html = template('slidesTpl', {data: res});
		$('#slidesBox').html(html);
	}
})

//删除按钮
$('#slidesBox').on('click','.delete',function(){   
    //判断是否确定删除
    if(confirm('确定删除么')){      
        var id = $(this).attr('data-id');
        $.ajax({
            type:'delete',
            url:'http://localhost:3000/slides/' + id,
            success: function(){
                location.reload();
            }
        })
    }
})